package team;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;


/**
 * User Interface for part selection and/or modification
 * @author Sean Peng
 *
 */
public class PartFrame extends JFrame{

		private JFrame frame = this;
		
		private DefaultListModel<String> vendorListModel;
		private DefaultListModel<String> partListModel;
		private JTable partInfoTable;
		private String vendor;
		private String partNum;
		
		/**
		 * Constructor for PartFrame
		 * @param previousFrame
		 */
		public PartFrame(final JFrame previousFrame, final Integer rLink, Object dummy) {
			
			vendorListModel = new DefaultListModel<String>();
			partListModel = new DefaultListModel<String>();			
			partInfoTable = new JTable() {
				
				public boolean isCellEditable(int rowIndex, int colIndex) {
					return false;
				}
			};
			
			JButton addButton = new JButton("Add");
			JButton backButton = new JButton("Back");
			
			addButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					
					Result.updateRADCRX(vendor, partNum, rLink);
				}
				
			});
			
			backButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					
					previousFrame.setVisible(true);
					frame.dispose();
				}
			
			});
			
			String[] vendors = Result.getVendor();
			
			for(String v : vendors)
				vendorListModel.addElement(v);
			
			JList<String> vendorList = new JList<String>(vendorListModel);			
			vendorList.setBorder(BorderFactory.createTitledBorder("Available Vendor:"));
			vendorList.addListSelectionListener(new ListSelectionListener() {

				@Override
				public void valueChanged(ListSelectionEvent e) {
					
					//only fire up when mouse up
					if(!e.getValueIsAdjusting()) {
						
						@SuppressWarnings("unchecked")
						JList<String> list = (JList<String>)e.getSource();
						vendor = list.getSelectedValue();
						partListModel.clear();
						
						String[] parts = Result.getPartNumber(vendor);
						for(String part : parts)
							partListModel.addElement(part);
					}
				}		
			});
			
			JList<String> partList = new JList<String>(partListModel);		
			partList.setBorder(BorderFactory.createTitledBorder("Available Part:"));
			partList.addListSelectionListener(new ListSelectionListener() {

				@Override
				public void valueChanged(ListSelectionEvent e) {
					
					//only fire up when mouse up
					if(!e.getValueIsAdjusting() && ((JList<String>)e.getSource()).getModel().getSize() != 0) {
						
						@SuppressWarnings("unchecked")
						JList<String> list = (JList<String>)e.getSource();
						partNum = list.getSelectedValue();
						
						try {
							Integer.parseInt(partNum);
							
							String[] spec = Result.getPartSpec(vendor, partNum);
							constructDescriptionTable(vendor, spec);
							
						} catch (Exception ex) {
							System.err.println(ex.getMessage());
						}
					}
				}
				
			});
			
			JScrollPane vendorScroll = new JScrollPane(vendorList);
			JScrollPane partScroll = new JScrollPane(partList);
			JScrollPane specScroll = new JScrollPane(partInfoTable);
			
			vendorScroll.setPreferredSize(new Dimension(150, 115));
			partScroll.setPreferredSize(new Dimension(150, 115));
			specScroll.setPreferredSize(new Dimension(500, 115));
			
			specScroll.setBorder(BorderFactory.createTitledBorder("Part Description: "));
			
			JPanel partSelection = new JPanel();
			partSelection.add(vendorScroll, BorderLayout.WEST);
			partSelection.add(partScroll, BorderLayout.EAST);
			
			JPanel buttonPanel = new JPanel();
			buttonPanel.add(addButton);
			buttonPanel.add(backButton);
			
			this.setSize(600,400);
			this.setTitle("Part Selection");
			this.add(partSelection, BorderLayout.NORTH);
			this.add(specScroll, BorderLayout.CENTER);
			this.add(buttonPanel, BorderLayout.SOUTH);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		} //End of Constructor
		
		
		/**
		 * Constructor for PartFrame
		 * @param autoFrame
		 * @param rLink
		 */
		public PartFrame(final JFrame autoFrame, final Integer rLink) {
			
			vendorListModel = new DefaultListModel<String>();
			partListModel = new DefaultListModel<String>();			
			partInfoTable = new JTable() {
				
				public boolean isCellEditable(int rowIndex, int colIndex) {
					return false;
				}
			};
			
			JButton backButton = new JButton("Back");
			
			backButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					
					autoFrame.setVisible(true);
					frame.dispose();
				}
			
			});
			
			String[] vendors = Result.getVendor(rLink);
			
			for(String v : vendors)
				vendorListModel.addElement(v);
			
			JList<String> vendorList = new JList<String>(vendorListModel);			
			vendorList.setBorder(BorderFactory.createTitledBorder("Available Vendor:"));
			vendorList.addListSelectionListener(new ListSelectionListener() {

				@Override
				public void valueChanged(ListSelectionEvent e) {
					
					//only fire up when mouse up
					if(!e.getValueIsAdjusting()) {
						
						@SuppressWarnings("unchecked")
						JList<String> list = (JList<String>)e.getSource();
						vendor = list.getSelectedValue();
						partListModel.clear();
						
						String[] parts = Result.getPartNumber(vendor, rLink);
						for(String part : parts)
							partListModel.addElement(part);
					}
				}		
			});
			
			JList<String> partList = new JList<String>(partListModel);		
			partList.setBorder(BorderFactory.createTitledBorder("Available Part:"));
			partList.addListSelectionListener(new ListSelectionListener() {

				@Override
				public void valueChanged(ListSelectionEvent e) {
					
					//only fire up when mouse up
					if(!e.getValueIsAdjusting() && ((JList<String>)e.getSource()).getModel().getSize() != 0) {
						
						@SuppressWarnings("unchecked")
						JList<String> list = (JList<String>)e.getSource();
						partNum = list.getSelectedValue();
						
						try {
							Integer.parseInt(partNum);
							
							String v = "";						
							if(vendor.contains("ARS"))
								v = "RDIMARS";
							else if(vendor.contains("BRH"))
								v = "RDIMBEH";
							else if(vendor.contains("DAN"))
								v = "RDIMDAN";
							else
								v = "RDIMMOD";
							
							String[] spec = Result.getPartSpec(v, partNum);
							constructDescriptionTable(v, spec);
							
						} catch (Exception ex) {
							System.err.println(ex.getMessage());
						}
					}
				}
				
			});
			
			JScrollPane vendorScroll = new JScrollPane(vendorList);
			JScrollPane partScroll = new JScrollPane(partList);
			JScrollPane specScroll = new JScrollPane(partInfoTable);
			
			vendorScroll.setPreferredSize(new Dimension(150, 115));
			partScroll.setPreferredSize(new Dimension(150, 115));
			specScroll.setPreferredSize(new Dimension(500, 115));
			
			specScroll.setBorder(BorderFactory.createTitledBorder("Part Description: "));
			
			JPanel partSelection = new JPanel();
			partSelection.add(vendorScroll, BorderLayout.WEST);
			partSelection.add(partScroll, BorderLayout.EAST);
			
			JPanel buttonPanel = new JPanel();
			buttonPanel.add(backButton);
			
			this.setSize(600,400);
			this.setTitle("Part Selection");
			this.add(partSelection, BorderLayout.NORTH);
			this.add(specScroll, BorderLayout.CENTER);
			this.add(buttonPanel, BorderLayout.SOUTH);
			this.setLocationRelativeTo(null);
			this.setVisible(true);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		} //End of Constructor

		
		
		/**
		 * Construct a table to display part information
		 * @param vendor name for the vendor's table
		 * @param data part information data
		 */
		private void constructDescriptionTable(String vendor, String[] data) {		 
			
			DefaultTableModel partInfoModel = (DefaultTableModel) partInfoTable.getModel();
			
			partInfoModel.setColumnCount(0);
			partInfoModel.addColumn("Part", Result.getColumnName(vendor));	
			partInfoModel.addColumn("Spec", data);
			partInfoModel.fireTableDataChanged();
			
		}
}
